package com.qishenyuan.competition.common;

import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @author qishenyuan
 */
public class Response<T> implements Serializable {

    private static final long serialVersionUID = 8476144817214924391L;

    /**
     * 错误码
     */
    private int code;

    /**
     * 信息
     */
    private String msg;

    /**
     * 错误信息
     */
    private String error;

    /**
     * 数据
     */
    private T data;

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getMsg() {
        return msg;
    }

    public void setMsg(String msg) {
        this.msg = msg;
    }

    public String getError() {
        return error;
    }

    public void setError(String error) {
        this.error = error;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }

    public static <T> Response<T> ok() {
        return build(null, ResponseCode.SUCCESS, (String) null);
    }

    public static <T> Response<T> ok(T data) {
        return build(data, ResponseCode.SUCCESS, (String) null);
    }

    public static <T extends Map> Response<? extends Map> ok(String key, Object value) {
        HashMap data = new HashMap<>();
        data.put(key, value);
        return build(data, ResponseCode.SUCCESS, (String) null);
    }

    public static <T> Response<T> ok(T data, String msg) {
        return build(data, ResponseCode.SUCCESS, msg);
    }

    public static <T> Response<T> failed() {
        return build( null, ResponseCode.FAIL, (String) null);
    }

    public static <T> Response<T> failed(String msg) {
        return build(null, ResponseCode.FAIL, msg);
    }

    public static <T> Response<T> failed(T data, String msg) {
        return build(data, ResponseCode.FAIL, msg);
    }

    public static <T> Response<T> failed(T data, int code, String msg) {
        return build(data, code, msg);
    }

    public static <T> Response<T> failed(int code, String msg) {
        return build(null, code, msg);
    }

    public static <T> Response<T> failed(int code, String msg, String error, T errorData) {
        return build(null, code, msg, error, errorData);
    }

    private static <T> Response<T> build(T data, int code, String msg) {
        return build(data, code, msg, null, null);
    }

    public static <T extends Map> Response<? extends Map> ok(PageResult pageData) {
        HashMap data = new HashMap();
        data.put("pageNumber", pageData.getPageNumber());
        data.put("pageSize", pageData.getPageSize());
        data.put("totalPage", pageData.getTotalPage());
        data.put("totalCount", pageData.getTotalCount());
        data.put("list", pageData.getList());
        return build(data, ResponseCode.SUCCESS, (String) null);
    }

    private static <T> Response<T> build(T data, int code, String msg, String error, T errorData) {
        Response<T> apiResult = new Response<>();
        apiResult.setCode(code);
        apiResult.setData(data);
        apiResult.setMsg(msg);
        apiResult.setError(error);
        return apiResult;
    }
}

